﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace weba.Sales
{
    public partial class MaterialInventoryShow : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
               // UART.BLL.Sales.Material bll = new UART.BLL.Sales.Material();
                //设置经办人
            Accounts_Users currentUser = mk.l.Accounts_Users.FirstOrDefault();
                //设置根部门ID
                var bllDept = mk.l.Accounts_Department.ToList();
                this.lblRootDepartmentID.Text = bllDept.Where(j => j.ClassID == (int.Parse(currentUser.DepartmentID))).FirstOrDefault().RootID.ToString();
                this.lblStrWhere.Text = " RootDepartmentID=" + this.lblRootDepartmentID.Text;
               // this.AspNetPager.RecordCount = bll.GetCount(this.lblStrWhere.Text);
                this.BindMaterialCategoryID();
                this.BindLocationID();
                this.BindData();
                //设置分页页数
                this.txtPageSize.Attributes.Add("onkeydown", "javascript:SetFocusPageSize();");
                //判断一下用户是否有修改库存的权限
               
            }
        }
        private void BindMaterialCategoryID()
        {
          //  UART.BLL.Sales.MaterialCategory bll = new UART.BLL.Sales.MaterialCategory();
            var ds = mk.l.Sales_MaterialCategory.ToList();
            this.ddlMaterialCategoryID.DataSource = ds;
            this.ddlMaterialCategoryID.DataTextField = "Name";
            this.ddlMaterialCategoryID.DataValueField = "MaterialCategoryID";
            this.ddlMaterialCategoryID.DataBind();
            ListItem li = new ListItem("不限", "0");
            this.ddlMaterialCategoryID.Items.Add(li);
            this.ddlMaterialCategoryID.Items.FindByValue("0").Selected = true;
        }

        /// <summary>
        /// Binds the location ID.绑定仓库列表
        /// </summary>
        private void BindLocationID()
        {
            var bll = mk.l.Sales_Location.ToList();
            var ds = bll.Where(y=>y.RootDepartmentID.ToString()==this.lblRootDepartmentID.Text).ToList();
            this.ddlLocationID.DataSource = ds;
            this.ddlLocationID.DataTextField = "Name";
            this.ddlLocationID.DataValueField = "LocationID";
            this.ddlLocationID.DataBind();
            ListItem li = new ListItem("不限", "0");
            this.ddlLocationID.Items.Add(li);
            this.ddlLocationID.Items.FindByValue("0").Selected = true;
        }

        /// <summary>
        /// 绑定订单列表
        /// </summary>
        private void BindData()
        {
           var bll = mk.l.Sales_MaterialInventory.ToList();
          //  DataSet ds = bll.GetList(this.AspNetPager.PageSize, this.AspNetPager.CurrentPageIndex, this.lblStrWhere.Text, "1");
           this.myGridView.DataSource = bll;
            this.myGridView.DataBind();
        }

        /// <summary>
        /// 分页绑定数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager_PageChanged(object sender, EventArgs e)
        {
            this.BindData();
        }

        /// <summary>
        /// Handles the Click event of the btnSetPageSize control.设置分页页数
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnSetPageSize_Click(object sender, EventArgs e)
        {
            if (this.txtPageSize.Text.Trim() != "")
            {
                if (this.txtPageSize.Text.Trim() != "0")
                {
                  //  this.AspNetPager.PageSize = int.Parse(this.txtPageSize.Text.Trim());
                    this.BindData();
                }
                else
                {
                  //  this.AspNetPager.PageSize = 10;
                    this.BindData();
                }
            }
            else
            {
               // this.AspNetPager.PageSize = 10;
                this.BindData();
            }
        }

        /// <summary>
        /// 绑定数据时显示状态信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //显示材料名称 类型
                int intMaterialID = int.Parse(this.myGridView.DataKeys[e.Row.RowIndex].Values["MaterialID"].ToString());
                Label lblMaterialName = (Label)(e.Row.FindControl("lblMaterialName"));
                Label lblMaterialCategoryName = (Label)(e.Row.FindControl("lblMaterialCategoryName"));

                var bllMaterial = mk.l.Sales_Material.ToList();
              Sales_Material mMaterial = bllMaterial.Where(h=>h.MaterialID==intMaterialID).FirstOrDefault();
                if (mMaterial != null)
                {
                    lblMaterialName.Text = mMaterial.Name;
                 var bllMaterialCategory =mk.l.Sales_MaterialCategory.ToList();
                    Sales_MaterialCategory mMaterialCategory = bllMaterialCategory.Where(h=>h.MaterialCategoryID==mMaterial.MaterialCategoryID).FirstOrDefault();
                    if(mMaterialCategory != null)
                    {
                        lblMaterialCategoryName.Text = mMaterialCategory.Name;
                    }
                }

                //显示仓库名称
                Label lblLocationName = (Label)(e.Row.FindControl("lblLocationName"));
                var  bllLocation =mk.l.Sales_Location.ToList();
             Sales_Location mLocation = bllLocation.Where(h=>h.LocationID==int.Parse(this.myGridView.DataKeys[e.Row.RowIndex].Values["LocationID"].ToString())).FirstOrDefault();
                if(mLocation != null)
                {
                    lblLocationName.Text = mLocation.Name;
                }


                //让数字以文本形式表示
                //e.Row.Cells[0].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
                //e.Row.Cells[1].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
                //e.Row.Cells[3].Attributes.Add("style", "vnd.ms-excel.numberformat:@");

            }
        }

        /// <summary>
        /// Handles the Click event of the btnSearch control.查询相关订单信息
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.lblStrWhere.Text = " RootDepartmentID=" + this.lblRootDepartmentID.Text;
            //设置查询条件
            //所有的条件都加and 最后再去掉就行了 tnnd早想到这个办法多好啊 不用写那么多if了
            //设置材料的查询条件
            StringBuilder strWhere = new StringBuilder();
            if (this.txtName.Text.Trim() != "")
            {
                strWhere.AppendFormat(" and Name like '%{0}%'", mk.InputText(this.txtName.Text, 50));
            }
            if (this.ddlMaterialCategoryID.SelectedValue != "0")
            {
                strWhere.Append(" and MaterialCategoryID=" + this.ddlMaterialCategoryID.SelectedValue);
            }

            StringBuilder strWhere2 = new StringBuilder();


            if (this.ddlLocationID.SelectedValue != "0")
            {
                strWhere2.Append(" and LocationID=" + this.ddlLocationID.SelectedValue);
            }

            StringBuilder strWhere3 = new StringBuilder();
            if (strWhere2.Length > 0)
            {
                strWhere3.Append(strWhere2);
            }
            if (strWhere.Length > 0)
            {
                strWhere3.Append(" and MaterialID in (select MaterialID from Sales_Material where ");
                strWhere3.Append(strWhere.ToString().Substring(5, strWhere.Length - 5));
                strWhere3.Append(")");
            }
            if (strWhere3.Length > 0)
            {
                //this.lblStrWhere.Text = strWhere3.ToString().Substring(5, strWhere3.Length - 5);
                this.lblStrWhere.Text += strWhere3.ToString();
            }
         //   UART.BLL.Sales.MaterialInventory bll = new UART.BLL.Sales.MaterialInventory();
          //  this.AspNetPager.RecordCount = bll.GetCount(this.lblStrWhere.Text);
            this.BindData();
        }

        /// <summary>
        /// Handles the RowEditing event of the myGridView control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewEditEventArgs"/> instance containing the event data.</param>
        protected void myGridView_RowEditing(object sender, GridViewEditEventArgs e)
        {
            this.myGridView.EditIndex = e.NewEditIndex;
            this.BindData();
        }

        /// <summary>
        /// Handles the RowCancelingEdit event of the myGridView control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewCancelEditEventArgs"/> instance containing the event data.</param>
        protected void myGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            this.myGridView.EditIndex = -1;
            this.BindData();
        }

        /// <summary>
        /// Handles the RowUpdating event of the myGridView control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewUpdateEventArgs"/> instance containing the event data.</param>
        protected void myGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            if (this.IsValid)
            {
             var  bll = mk.l.Sales_MaterialInventory.ToList();
               Sales_MaterialInventory m = bll.Where(h=>h.RecordID==int.Parse(this.myGridView.DataKeys[e.RowIndex].Values["RecordID"].ToString())).FirstOrDefault();
                TextBox txtQuantity = this.myGridView.Rows[e.RowIndex].FindControl("txtQuantity") as TextBox;
                m.Quantity = decimal.Parse(txtQuantity.Text);
              //  bll.Update(m);
                mk.l.SubmitChanges();
                this.myGridView.EditIndex = -1;
                this.BindData();
            }
        }

    }
}